<?php
/**
 * 城市数据模型
 * Created by Sublime Text 3.
 * @authors Rejeo (rejeo@qq.com)
 * @date    2018-04-22 19:33:31
 */
namespace app\common\model;

use think\Model;

class City extends Model
{
    // 添加地址
    public function add($data)
    {
        $data['status'] = 1;
        $data['create_time'] = time();
        return $this->save($data);
    }

    // 查询地址
    public function getNormalFirstCity()
    {
        $data = [
            'status'    =>1,
            'parent_id' =>0,
        ];
        $order = [
            // 'listorder' => 'desc',
            'id' => 'desc',
        ];
        $res = $this->where($data)
                    ->order($order)
                    ->select();
        return $res;
    }


    // 获取顶级地址
    public function getNormalCitysByParentId($parentId=0)
    {
        // dump($parentId);exit;
        $data = [
            'parent_id' => $parentId,
            'status' => ['gt',-1],
        ];
        $order = [
            'id' => 'desc',
        ];
        $res = $this->where($data)
                    ->order($order)
                    ->paginate();
        return $res;
    }

    // 获取父地址
    public function getCitysToParentId($parentId=0)
    {
        $data  = [
            'parent_id' => $parentId,
            'status' => 1,
        ];
        $order = [
            'id' => 'desc',
        ];
        // 只选取要使用到的字段
        $field = [
            'id','name','uname','parent_id'
        ];
        $res = $this->where($data)
                    ->field($field)
                    ->order($order)
                    ->select();
        return $res;
    }

    // 获取父地址
    public function getNormalCitys()
    {
        $data  = [
            'parent_id' => ['gt',0],
            'status' => 1,
        ];
        $order = [
            'id' => 'desc',
        ];
        // 只选取要使用到的字段
        $field = [
            'id','name','uname','parent_id','is_default'
        ];
        $res = $this->where($data)
                    ->field($field)
                    ->order($order)
                    ->select();
        return $res;
    }

}
